/**
 * https://leetcode.cn/problems/relative-ranks/
 * @param score
 */
function findRelativeRanks(score: number[]): string[] {
  const ans: string[] = [];
  const map: Record<number, number> = {};
  for (let i = 0; i < score.length; i++) {
    map[score[i]] = i;
  }

  // 从大到小排序
  score.sort((a, b) => b - a)

  const namesMap = ["Gold Medal", "Silver Medal", "Bronze Medal"];

  for (let i = 0; i < score.length; i++) {
      ans[map[score[i]]] = i < 3 ? namesMap[i] : (i + 1).toString()
  }

  return ans;
}

function findRelativeRanks2(score: number[]): string[] {
    const ans: string[] = [];
    const map: Record<number, number> = {};
    for (let i = 0; i < score.length; i++) {
      map[score[i]] = i;
    }
  
    const keys = Object.keys(map);
    const namesMap = ["Gold Medal", "Silver Medal", "Bronze Medal"];
    let index = 0;
    for (let i = keys.length - 1; i >= 0; i--) {
      ans[map[+keys[i]]] = index < 3 ? namesMap[index++] : (++index).toString();
    }
  
    return ans;
  }

console.log(findRelativeRanks([5, 4, 3, 2, 1]));
